class BackendController < ApplicationController

  wsdl_service_name 'Backend'
	web_service_api ArticleApi
	web_service_scaffold :invoke
	
	def upd_cat1(name,newname)
		@categn = Category.find_by_name(name)
    if @categn		   	   
	   @categn.name = newname
	   @categn.save
	  end
	  return true
	end
	
  def del_cat1(name)
  		Category.find(:all,:conditions=>['categories.name = ?',name]).each do |cat1obj|

	   cat1obj.destroy
	  
	  end 
     return true
  end	
  
   def ins_cat1(name)
   	   @categn = Category.new
	   @categn.name = name
	   @categn.save
  end 
  
	def upd_cat2(name,newname)
		@categnt = Categoryt.find_by_name(name)
    if @categnt		   	   
	   @categnt.name = newname
	   @categnt.save
	  end
	  return true
	end
	
  def del_cat2(name)
  		Categoryt.find(:all,:conditions=>['categoryts.name = ?',name]).each do |cat2obj|

	   cat2obj.destroy
	  
	  end 
     return true
  end	
  
   def ins_cat2(name)
   	   @categnt = Categoryt.new
	   @categnt.name = name
	   @categnt.save
  end   
	
	def upd_article(orititle,title,synopsis,body,user_published_by,category_f,categoryt_f,tagn,media_url)
	

		  begin
		  			@articleobj = Article.find_by_title(orititle.strip)

      rescue ActiveRecord::RecordNotFound
        @articleobj = nil
      end		
      
      
		
    if @articleobj.nil?

		 else
		  @curr_categ = Category.find_by_name(category_f)
		  
		  @curr_categ_id = 4
		  if @curr_categ
		   @curr_categ_id = @curr_categ.id
		  else
		   @categn = Category.new
		   @categn.name = category_f
		   @categn.save
		   @curr_categ_id = @categn.id
		  end
		  
		  @curr_categt = Categoryt.find_by_name(categoryt_f)
		  
		  @curr_categt_id = 1
		  if @curr_categt
		   @curr_categt_id = @curr_categt.id
		  else
		   @categnt = Categoryt.new
		   @categnt.name = categoryt_f
		   @categnt.save
		   @curr_categt_id = @categnt.id
		  end		 
		  
    @curr_tagid = 1
		  begin	  
			  @curr_tag = Tag.find_by_name(tagn)	  
      rescue ActiveRecord::RecordNotFound
        @curr_tag = nil
      end	  
      
	  if @curr_tag.nil?   
	   @currtag = Tag.new
	   @currtag.name = tagn
	   @currtag.save
	   @curr_tagid = @currtag.id
	  else
	   @curr_tagid = @curr_tag.id
	  end  		  
		 
		  @articleobj.title = title
		  @articleobj.synopsis = synopsis
		  @articleobj.body = body
		  @articleobj.user_published_by = user_published_by
		  @articleobj.category_id = @curr_categ_id
		  @articleobj.categoryt_id = @curr_categt_id 
		  @articleobj.tag_id = @curr_tagid
		  @articleobj.media_url = media_url
		  @articleobj.save		 
		 end
	   
     return true
	end
	
  def del_article(title)
	
		Article.find(:all,:conditions=>['articles.title = ?',title]).each do |articleobj|

	   articleobj.destroy
	  
	  end 
     return true
	end
	
  def ins_article(title,synopsis,body,user_published_by,media_url,email_addr,category_f,photou,uprofile,categoryt_f,tagn)
	
		@articleobj = Article.new

	  @articleobj.title = title
	  @articleobj.synopsis = synopsis
	  @articleobj.body = body
	  @articleobj.user_published_by = user_published_by

    @curr_tagid = 1
		  begin	  
			  @curr_tag = Tag.find_by_name(tagn)	  
      rescue ActiveRecord::RecordNotFound
        @curr_tag = nil
      end	  
      
	  if @curr_tag.nil?   
	   @currtag = Tag.new
	   @currtag.name = tagn
	   @currtag.save
	   @curr_tagid = @currtag.id
	  else
	   @curr_tagid = @curr_tag.id
	  end   
	  
	  @curr_categ = Category.find_by_name(category_f)
	  
	  @curr_categ_id = 4
	  if @curr_categ
	   @curr_categ_id = @curr_categ.id
	  else
	   @categn = Category.new
	   @categn.name = category_f
	   @categn.save
	   @curr_categ_id = @categn.id
	  end
	  
	  @curr_categt = Categoryt.find_by_name(categoryt_f)
	  
	  @curr_categt_id = 1
	  if @curr_categt
	   @curr_categt_id = @curr_categt.id
	  else
	   @categnt = Categoryt.new
	   @categnt.name = categoryt_f
	   @categnt.save
	   @curr_categt_id = @categnt.id
	  end	  

		  begin
		  	    @curr_name = Person.find_by_name(user_published_by)	

      rescue ActiveRecord::RecordNotFound
        @curr_name = nil
      end
  
	  @curr_name_id = 1	  
	  
	  if @curr_name.nil?
	  	  
	   @currnam = Person.new
	   @currnam.name = user_published_by
	   @currnam.email = email_addr	  
	   @currnam.password = "6666"	 	   
	   @currnam.password_confirmation = "6666"	 		    
	   @currnam.uphoto = photou
	   @currnam.uflag = true	
	   @currnam.description = uprofile	    	   
	   @currnam.save
	   @curr_name_id = @currnam.id	  	  
 
	  else
	   @curr_name_id = @curr_name.id  	 
	  end


	  
	  @articleobj.published = true

	  @articleobj.person_id =  @curr_name_id
	  @articleobj.categoryt_id =  @curr_categt_id	  
	  @articleobj.category_id =  @curr_categ_id	  
	  @articleobj.tag_id = @curr_tagid
	  
	  @articleobj.media_url = media_url
	  	
	  @articleobj.save
		
	end
	
	def upd_comm(artictitle,oribody,body)
		  begin
		  	    @curr_artic = Article.find_by_title(artictitle)	

      rescue ActiveRecord::RecordNotFound
        @curr_artic = nil
      end	
      
       @curr_artic_id = 0      
      if @curr_artic.nil?
       @curr_artic_id = 0
      else
       @curr_artic_id = @curr_artic.id
      end
	
		@articommobj = ArticleComment.find_by_article_id_and_body( @curr_artic_id,oribody)
    if @articommobj		   	   
	   @articommobj.body = body
	   @articommobj.save
	  end
	  return true
	end
	
  def del_comm(artictitle,body)
  		  begin
		  	    @curr_artic = Article.find_by_title(artictitle)	

      rescue ActiveRecord::RecordNotFound
        @curr_artic = nil
      end	
      
       @curr_artic_id = 0      
      if @curr_artic.nil?
       @curr_artic_id = 0
      else
       @curr_artic_id = @curr_artic.id
      end
      
  		ArticleComment.find(:all,:conditions=>['article_comments.body = ? and article_comments.article_id = ?',body,@curr_artic_id]).each do |articommobj|

	   articommobj.destroy
	  
	  end 
     return true
  end	
  
   def ins_comm(artictitle,body,user_published_by,email_addr,photou,uprofile)
  		  begin
		  	    @curr_artic = Article.find_by_title(artictitle)	

      rescue ActiveRecord::RecordNotFound
        @curr_artic = nil
      end	
      
       @curr_artic_id = 0      
      if @curr_artic.nil?
       @curr_artic_id = 0
      else
       @curr_artic_id = @curr_artic.id
      end   
   
		  begin
		  	    @curr_name = Person.find_by_name(user_published_by)	

      rescue ActiveRecord::RecordNotFound
        @curr_name = nil
      end
  
	  @curr_name_id = 1	  
	  
	  if @curr_name.nil?
	  	  
	   @currnam = Person.new
	   @currnam.name = user_published_by
	   @currnam.email = email_addr	  
	   @currnam.password = "6666"	 	   
	   @currnam.password_confirmation = "6666"	 		    
	   @currnam.uphoto = photou
	   @currnam.uflag = true	
	   @currnam.description = uprofile	    	   
	   @currnam.save
	   @curr_name_id = @currnam.id	  	  
 
	  else
	   @curr_name_id = @curr_name.id  	 
	  end   
   
   
   	   @articomm = ArticleComment.new
   	   @articomm.person_id = @curr_name_id
   	   @articomm.article_id = @curr_artic_id   	   
	   @articomm.body = body
	   @articomm.save
  end 
end
